package com.xyf.listener;

import com.xyf.pojo.ProductType;
import com.xyf.service.ProductTypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

// ServletContextListener全局监听器
@WebListener
public class ProductTypeListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 手工从Spring容器中取出ProductTypeServiceImpl的对象。
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-*.xml");
        // 取出ProductTypeServiceImpl
        ProductTypeService productTypeService = (ProductTypeService) context.getBean("ProductTypeServiceImpl");
        // 获取所有商品的类别
        List<ProductType> typeList = productTypeService.getAll();
        // 放入全局应用作用域中，供新增页面，修改页面，前台的查询功能提供全部商品类别集合
        servletContextEvent.getServletContext().setAttribute("typeList",typeList);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
